package com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo;

import cn.hutool.core.util.StrUtil;
import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.common.core.constant.StringPool;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author chenms
 * @date 2023/10/7 5:07 PM
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class OrderConfirmResultBO implements Serializable {

    /**
     * 确认失败信息
     */
    private List<ConfirmFailureBO> failedList;

    /**
     * 确认成功信息
     */
    private List<ConfirmSuccessBO> confirmSuccessList;

    public void appendFailedList(List<ConfirmFailureBO> list) {
        if (Objects.isNull(list)) {
            return;
        }
        if (Objects.isNull(failedList)) {
            failedList = Lists.newArrayList();
        }
        failedList.addAll(list);
    }

    public void appendSuccessList(List<ConfirmSuccessBO> list) {
        if (Objects.isNull(list)) {
            return;
        }
        if (Objects.isNull(confirmSuccessList)) {
            confirmSuccessList = Lists.newArrayList();
        }
        confirmSuccessList.addAll(list);
    }

    public void append(OrderConfirmResultBO orderConfirmResult) {
        if (Objects.nonNull(orderConfirmResult)) {
            this.appendFailedList(orderConfirmResult.getFailedList());
            this.appendSuccessList(orderConfirmResult.getConfirmSuccessList());
        }
    }

    /**
     * 从渠道确认订单结果中获取订单号和渠道交易号的映射关系
     * @return
     */
    public Map<String, String> getOrderSnChannelTradeSnMap() {
        HashMap<String, String> map = Maps.newHashMap();
        if(CollectionUtils.isNotEmpty(confirmSuccessList)) {
            confirmSuccessList.forEach(item -> {
                if(StrUtil.isNotBlank(item.getChannelTradeOrderSn())) {
                    map.put(item.getOrderSn(), item.getChannelTradeOrderSn());
                }
            });
        }
        if(CollectionUtils.isNotEmpty(failedList)) {
            failedList.forEach(item -> {
                if(StrUtil.isNotBlank(item.getChannelTradeOrderSn())) {
                    map.put(item.getOrderSn(), item.getChannelTradeOrderSn());
                }
            });
        }
        return map;
    }


    /**
     * 确认失败信息
     */
    @Data
    public static class ConfirmFailureBO implements Serializable {
        /**
         * 渠道编码
         */
        private String channelCode;

        /**
         * 渠道交易订单号
         */
        private String channelTradeOrderSn;

        /**
         * 供应链订单号
         */
        private String orderSn;

        /**
         * 供应链三方交易单号
         */
        private String orderTradeSn;

        /**
         * 异常编码
         */
        private String errorCode;

        /**
         * 异常信息
         */
        private String errorMessage;

        /**
         * 异常数据
         */
        private Object data;

        /**
         * 创建订单阶段
         *
         * @see com.duobaoyu.dby.channel.adapter.enums.biz.order.OrderCreateStageEnum
         */
        private String stage;

        /**
         * 获取异常信息截取前200个字符
         */
        public String getTruncationErrorInfo() {
            int maxLength = CommonConstants.HUNDRED * CommonConstants.TWO;
            StringBuilder errorMsgBuilder = new StringBuilder();
            if (Objects.nonNull(errorMessage)) {
                errorMsgBuilder.append(errorMessage);
            }
            if (ObjectUtils.isNotEmpty(data)) {
                errorMsgBuilder.append(StringPool.BRACKET_START).append(data).append(StringPool.BRACKET_END);
            }
            if (errorMsgBuilder.length() > maxLength) {
                errorMsgBuilder.setLength(maxLength);
            }
            return errorMsgBuilder.toString();
        }
    }

    @Data
    public static class ConfirmSuccessBO implements Serializable {
        /**
         * 渠道编码
         */
        private String channelCode;

        /**
         * 渠道交易订单号
         */
        private String channelTradeOrderSn;

        /**
         * 供应链订单号
         */
        private String orderSn;

        /**
         * 供应链三方交易单号
         */
        private String orderTradeSn;

        /**
         * 订单状态
         */
        private Integer status;

    }


}
